#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time    : 2022/7/17 9:06 下午
# @Author  : super_gou
# @File    : third.py

#函数
# def add_num2(a, b):
#     result = a + b
#     print(result)
# add_num2(10, 20)

# def sum_num(a, b):
#     return a + b
# result = sum_num(1, 2)
# print(result)

# def sum_num(a, b):
#     """ 求和函数 """
#     return a + b
# help(sum_num)
#
# def testB():
#     print('---- testB start----')
#     print('这⾥是testB函数执⾏的代码...(省略)...')
#     print('---- testB end----')
# def testA():
#     print('---- testA start----')
#     testB()
#     print('---- testA end----')
# testA()

# def print_line():
#     print('-' * 20)
# print_line()

# def print_line():
#     print('-' * 20)
# def print_lines(num):
#     i = 0
#     while i < num:
#         print_line()
#         i += 1
# print_lines(5)

# def sum_num(a, b, c):
#     return a + b + c
# result = sum_num(1, 2, 3)
# print(result)

# def average_num(a, b, c):
#     sumResult = sum_num(a, b, c)
#     return sumResult / 3
# result = average_num(1, 2, 3)
# print(result)

# def testA():
#     a = 100
#     print(a)
# testA() # 100
# print(a)

# def testA():
#     global a
#     a =100
#     print(a)
# testA()
# print(a)

# glo_num = 0
# def test1():
#     global glo_num
#     glo_num = 100
#     print(glo_num)
# def test2():
#     print(glo_num)
# test1()
# test2()

# def test1():
#     return 50
# def test2(num):
#     print(num)
# result = test1()
# test2(result)

# def return_num():
#     return 1, 2
# result = return_num()
# print(result)

#位置参数
# def user_info(name, age, gender):
#     print(f'您的名字是{name}, 年龄是{age}, 性别是{gender}')
# user_info('TOM', 20, '男')

#关键字参数
# def user_info(name, age, gender):
#     print(f'您的名字是{name}, 年龄是{age}, 性别是{gender}')
# user_info(name='Rose', age=20, gender='⼥')
# user_info('⼩明', gender='男', age=16)

#缺省参数
# def user_info(name, age, gender='男'):
#     print(f'您的名字是{name}, 年龄是{age}, 性别是{gender}')
# user_info('TOM', 20)
# user_info(name='Rose', age=18, gender='⼥')

# def user_info(*args):
#     print(args)
# user_info('TOM')
# user_info('TOM', 18)
# # ('TOM', 18)
# list1 = [1,2,3]
# user_info(*list1)

# def user_info(**kwargs):
#     print(kwargs)
# user_info(name='TOM', age=18, id=110)
# dict1= {'name': 'TOM', 'age': 18, 'id': 110}
# user_info(**dict1)

# def return_num():
#     return 100, 200
# num1, num2 = return_num()
# print(num1)
# print(num2)

# dict1 = {'name': 'TOM', 'age': 18}
# a, b = dict1
# print(a)
# print(b)
# print(dict1[a])
# print(dict1[b])

# a = 10
# b = 20
#方法一：通过第三变量替换
# c = 0
# c = a
# a = b
# b = c
# print(a)
# print(b)
#方法二：直接替换
# a, b = 1, 2
# a, b = b, a
# print(a)
# print(b)

# def test1(a):
#     print(a)
#     print(id(a))
#     a += a
#     print(a)
#     print(id(a))
# # int：计算前后id值不同
# b = 100
# test1(b)
# # 列表：计算前后id值相同
# c = [11, 22]
# test1(c)

#可变类型和不可变类型
# 所谓可变类型与不可变类型是指：数据能够直接进⾏修改，如果能直接修改那么就是可变，否则是不可
# 变.
# 可变类型 列表 字典 集合
# 不可变类型 整型 浮点型 字符串  元组

# f = open('testdata.txt', 'w')
# # 2.⽂件写⼊
# f.write('hello world')
# # 3. 关闭⽂件
# f.close()

# f = open('testdata.txt')
# content = f.readlines()
# print(content)
# # 关闭⽂件
# f.close()

# f = open('testdata.txt')
# content = f.readline()
# print(f'第⼀⾏：{content}')
# content = f.readline()
# print(f'第⼆⾏：{content}')
# 关闭⽂件
# f.close()
# seek()
# 作⽤：⽤来移动⽂件指针。
# ⽂件对象.seek(偏移量, 起始位置)


# old_name = input('请输⼊您要备份的⽂件名：')
# index = old_name.rfind('.')
# if index > 0:
#     postfix = old_name[index:]
# new_name = old_name[:index] + '[备份]' + postfix
# old_f = open(old_name, 'rb')
# new_f = open(new_name, 'wb')
# while True:
#     con = old_f.read(1024)
#     if len(con) == 0:
#         break
#     new_f.write(con)
# old_f.close()
# new_f.close()

import os
# flag = 1
# dir_name = './'
# file_list = os.listdir(dir_name)
# print(file_list)
# for name in file_list:
# # 添加指定字符
#     if flag == 1:
#         new_name = 'Python-' + name
# # 删除指定字符
#     elif flag == 2:
#         num = len('Python-')
#         new_name = name[num:]
# # 打印新⽂件名，测试程序正确性
#         print(new_name)
# # 重命名
# os.rename(dir_name+name, dir_name+new_name)

# def print_info():
#     print('-' * 20)
#     print('欢迎登录学员管理系统')
#     print('1: 添加学员')
#     print('2: 删除学员')
#     print('3: 修改学员信息')
#     print('4: 查询学员信息')
#     print('5: 显示所有学员信息')
#     print('6: 退出系统')
#     print('-' * 20)
# print_info()
# user_num = input('请选择您需要的功能序号：')
# if user_num == '1':
#     print('添加学员')
# elif user_num == '2':
#     print('删除学员')
# elif user_num == '3':
#     print('修改学员信息')
# elif user_num == '4':
#     print('查询学员信息')
# elif user_num == '5':
#     print('显示所有学员信息')
# elif user_num == '6':
#     print('退出系统')
# else:
#     print('输⼊错误，请重新输⼊!!!')
'''
info= []
def add_info():
    """ 添加学员 """
# 接收⽤户输⼊学员信息
    new_id = input('请输⼊学号：')
    new_name = input('请输⼊姓名：')
    new_tel = input('请输⼊⼿机号：')
# 声明info是全局变量
    global info
# 检测⽤户输⼊的姓名是否存在，存在则报错提示
    for i in info:
        if new_name == i['name']:
            print('该⽤户已经存在！')
            return
# 如果⽤户输⼊的姓名不存在，则添加该学员信息
    info_dict = {}
    # 将⽤户输⼊的数据追加到字典
    info_dict['id'] = new_id
    info_dict['name'] = new_name
    info_dict['tel'] = new_tel
# 将这个学员的字典数据追加到列表
    info.append(info_dict)
    print(info)

# 删除学员
def del_info():
    """删除学员"""
# 1. ⽤户输⼊要删除的学员的姓名
    del_name = input('请输⼊要删除的学员的姓名：')
    global info
# 2. 判断学员是否存在:如果输⼊的姓名存在则删除，否则报错提示
    for i in info:
        if del_name == i['name']:
            info.remove(i)
            break
        else:
            print('该学员不存在')
    print(info)

# 修改函数
def modify_info():
    """修改函数"""
# 1. ⽤户输⼊要修改的学员的姓名
    modify_name = input('请输⼊要修改的学员的姓名：')
    global info
# 2. 判断学员是否存在：如果输⼊的姓名存在则修改⼿机号，否则报错提示
    for i in info:
        if modify_name == i ['name']:
            i['tel'] = input('请输⼊新的⼿机号：')
            break
        else:
            print('该学员不存在')
    print(info)

# 查询学员
def search_info():
    """查询学员"""
# 1. 输⼊要查找的学员姓名：
    search_name = input('请输⼊要查找的学员姓名：')
    global info
# 2. 判断学员是否存在：如果输⼊的姓名存在则显示这位学员信息，否则报错提示
    for i in info:
        if search_name == i['name']:
            print('查找到的学员信息如下：----------')
            print(f"该学员的学号是{i['id']}, 姓名是{i['name']}, ⼿机号是{i['tel']}")
            break
        else:
            print('该学员不存在')

def print_all():
    """ 显示所有学员信息 """
    print('学号\t姓名\t⼿机号')
    for i in info:
        print(f'{i["id"]}\t{i["name"]}\t{i["tel"]}')
'''

#递归
# def sum_num(num):
#     if num == 1:
#         return 1
#     return num + sum_num(num-1)
# aa =sum_num(3)
# print(aa)

# def fn1():
#     return 200
# print(fn1)
# print(fn1())
# # lambda表达式
# fn2 = lambda: 100
# print(fn2)
# print(fn2())

# def add(a, b):
#     return a + b
# result = add(1, 2)
# print(result)

# fn1 = lambda a, b: a + b
# print(fn1(1, 2))

# fn1 = lambda: 100
# print(fn1())

# fn1 = lambda a: a
# print(fn1('hello world'))

# fn1 = lambda a, b, c=100: a + b + c
# print(fn1(10, 20))

# fn1 = lambda *args: args
# print(fn1(10, 20, 30))

# fn1 = lambda **kwargs: kwargs
# print(fn1(name='python', age=20))

# n1 = lambda a, b: a if a > b else b
# print(n1(1000, 500))


# def add_num(a, b):
#     return abs(a) + abs(b)
# result = add_num(-1, 2)
# print(result)

# def sum_num(a, b, f):
#     return f(a) + f(b)
# result = sum_num(-1, 2, abs)
# print(result)

'''
map(func, lst)，将传⼊的函数变量func作⽤到lst变量的每个元素中，并将结果组成新的列表(Python2)/
迭代器(Python3)返回
'''
# list1 = [1, 2, 3, 4, 5]
# def func(x):
#     return x ** 2
# result = map(func, list1)
# print(result)
# print(list(result))

import functools
'''
reduce(func，lst)，其中func必须有两个参数。每次func计算的结果继续和序列的下⼀个元素做累积计
算。
'''
# list1 = [1, 2, 3, 4, 5]
# def func(a, b):
#     return a + b
# result = functools.reduce(func, list1)
# print(result)

'''
ﬁlter(func, lst)函数⽤于过滤序列, 过滤掉不符合条件的元素, 返回⼀个 ﬁlter 对象。如果要转换为列表,
可以使⽤ list() 来转换。
'''
# list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# def func(x):
#     return x % 2 == 0
# result = filter(func, list1)
# print(result)
# print(list(result))

# 1.使用列表推导式生成能被5整除的数（100以内）
# num1 = [i for i in range(1,101) if i%5==0]
# print(num1)

# 2.有两个列表，一个是学生姓名，一个是学生的年龄，生成一个字典，key为姓名，value为年龄
list1=["zhangsan","lisi","wangwu"]
list2=[20,25,77]
dict1={list1[i]:list2[i] for i in range(len(list1))}
print(dict1)
# 3.开发一个注册系统，
# 页面：
# [{name:xxx,age:xxx},xxx]
# ----------------
# *   1-新增用户
# *   2-查询用户
# *   3-删除用户
# ----------------
# 功能1：新增学生信息（姓名和年龄）通过键盘，如果学生已经存在提示用户已存在
# 功能2：查询学生信息
# 功能3：删除学生信息
def  welcome_gui():
    """
    欢迎页面
    :return:
    """
    print("""

----------------
*   1-新增用户
*   2-查询用户
*   3-删除用户
*   4-修改用户
*   5-显示所有用户
*   6-退出系统
---------------- 

    """)

user_list=[]

def select_func(user_select):
    """
    :param   user_select: 用户输入的功能选择数字
    :return:  根据选择数字不同，调用不同功能函数
    """
    if user_select==1:
        print("---1-新增用户---")
        add_user()
    elif user_select == 2:
        print("---2-查询用户---")
        query_user()
    elif user_select == 3:
        print("---3-删除用户---")
        del_user()
    elif user_select == 4:
        print("---4-修改用户---")
        modify_user()
    elif user_select==5:
        print("---5-显示所有用户---")
        show_all_user()
    elif user_select==6:
        print("---6-退出系统---")
    else:
        print("输入错误，请输入1-6中的任一个整数！")

def add_user():
    """
    新增用户
    :return:
    """
    id=input("请输入学号：")
    for user in user_list:
        if id==user["id"]:
            print("新增用户，失败——学号重复，该用户已存在！")
            break
    else:
        name=input("请输入姓名：")
        age=input("请输入年龄：")
        user_dict={}
        user_dict["id"]=id
        user_dict["name"]=name
        user_dict["age"]=age
        user_list.append(user_dict)
        print("新增用户，成功！")

def query_user():
    """
    查询用户
    :return:
    """
    id=input("请输入学号：")
    for user in user_list:
        if id==user["id"]:
            print(f"查询的用户信息是：{user}")
            break
    else:
        print("学号不存在，查询失败！")

def  del_user():
    """
    删除用户
    :return:
    """
    id = input("请输入学号：")
    for user in user_list:
        if id == user["id"]:
            user_list.remove(user)
            print("删除成功！")
            break
    else:
        print("学号不存在，删除失败！")

def modify_user():
    """
    修改用户
    :return:
    """
    id=input("请输入【要修改信息】的学号：")
    for user in user_list:
        if id==user["id"]:
            user['name']=input("请输入【修改后】的姓名：")
            user['age']=input("请输入【修改后】的年龄：")
            print("修改成功！")
            break
    else:
        print("学号不存在，修改失败！")

def  show_all_user():
    """
    显示所有用户
    :return:
    """
    if user_list==[]:
        print("用户为空！")
    else:
        for user in user_list:
            print(f"所有用户信息为：{user}")


while True:
    welcome_gui()
    flag = True
    if len(user_list)<1:
        while flag:
            add_user()
            if len(user_list)>5:
                flag =False
    user_select=input("请输入1-6中间任一整数：")
    if user_select.isdigit() :  #只允许输入数字
        user_select=eval(user_select.strip().lstrip("0"))
        select_func(user_select)
        if user_select==6:
            flag=int(input("确定退出吗？1-退出 2-不退出："))
            if flag==1 or flag==2:
                if flag==1:
                    break
            else:
                print("请输入1或2！")
    else:
        print("输入错误，请输入1-6中的任一个整数！")